﻿using System.IO;
using NAnt.Core;
using NAnt.Core.Attributes;
using NAnt.Core.Types;

namespace NAnt.SVN.Tasks
{
    /// <summary>
    /// Implementation of the unlock command.
    /// You can run this tasc with the svnunlock tag in NAnt.
    /// $Id$
    /// </summary>
    [TaskName("svnunlock")]
    public class UnLock : SVNTask
    {
        #region Private Fields

        string targets = string.Empty;
        string user = string.Empty;
        string password = string.Empty;
        string revision = string.Empty;
        bool noauthcache = false;
        bool force = false;
        bool noninteractive = true;

        #endregion

        #region Public Fields

        /// <summary>
        /// The file to set the properties to
        /// </summary>
        [TaskAttribute("file", Required = false)]
        public virtual FileInfo File
        {
            get;
            set;
        }

        /// <summary>
        /// Username
        /// </summary>
        [TaskAttribute("user", Required = false)]
        [StringValidator()]
        public string User
        {
            get
            {
                return user;
            }
            set
            {
                user = value;
            }
        }

        /// <summary>
        /// Password
        /// </summary>
        [TaskAttribute("password", Required = false)]
        [StringValidator()]
        public string Password
        {
            get
            {
                return password;
            }
            set
            {
                password = value;
            }
        }

        /// <summary>
        /// If true, the authentication is not cached
        /// </summary>
        [TaskAttribute("noauthcache", Required = false)]
        [BooleanValidator()]
        public bool NoAuthCache
        {
            get
            {
                return noauthcache;
            }
            set
            {
                noauthcache = value;
            }
        }

        /// <summary>
        /// Force the unlock
        /// </summary>
        [TaskAttribute("force", Required = false)]
        [BooleanValidator()]
        public bool Force
        {
            get
            {
                return force;
            }
            set
            {
                force = value;
            }
        }

        /// <summary>
        /// Check if the checkout has to be performed with 
        /// authentication or not.
        /// </summary>
        public bool HasCredentials
        {
            get
            {
                return !string.IsNullOrEmpty(User) &&
                    !string.IsNullOrEmpty(Password);
            }
        }

        /// <summary>
        /// Run the command non interactive
        /// Currently always set to true.
        /// </summary>
        public bool NonInteractive
        {
            get
            {
                return noninteractive;
            }
        }

        #endregion

        /// <summary>
        /// Initialize command
        /// </summary>
        protected override void InitializeSVNTask()
        {
            Command = SVNCommands.UNLOCK;
            
            //Add the file
            Options.Add(SVNFlags.FILE);
            Options.Add(quote(File.FullName));

            //Add the credentials
            if (HasCredentials)
            {
                Options.Add(SVNFlags.USER);
                Options.Add(quote(User));
                Options.Add(SVNFlags.PASSWORD);
                Options.Add(quote(Password));
                if (NoAuthCache) Options.Add(SVNFlags.NOAUTHCACHE);
            }
            //Noninteractive and force attributes
            if (NonInteractive) Options.Add(SVNFlags.NONINTERACTIVE);
            if (Force) Options.Add(SVNFlags.FORCE);
        }
    }
}